﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Net;
using BLL;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using WFNetLib.ADO;

public partial class Traffic_Traffic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindLifeInfo();
            string[] typeList = new string[7];
            typeList[0] = "违法金额";
            typeList[1] = "号牌号码";
            typeList[2] = "违法时间";
            typeList[3] = "记分";
            typeList[4] = "缴款状态";
            typeList[5] = "违法地点";
            typeList[6] = "违法内容";

            for (int i = 0; i < 7; i++)
            {
                ListItem item = new ListItem();
                item.Text = typeList[i].ToString();
                item.Value = i + "," + typeList[i].ToString();

                item.Selected = true;
                //cboxshopInfo.Items.Add(item);
                //this.cboxshopInfo.Items[i].Text = this.cboxshopInfo.Items[i].Text.IndexOf("font") == -1 ? "<font color=’blue’ size='1px' >" + this.cboxshopInfo.Items[i].Text + "</font>" : this.cboxshopInfo.Items[i].Text.Replace("blue", "blue");
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string unit_num = ddlDy.SelectedValue + ddlXzqh.SelectedValue + txtHphm.Text;

        unit_logData unitLog = new unit_logData();
        unit_logBll unitLogBll = new unit_logBll();
        unitLog.unit_number = unit_num;
        unitLog.unit_shelf_number = txtClsbdh.Text.Trim();
        //unitLog.query_user_id = base.GetCurrentUserId();
        unitLog.query_time = DateTime.Now;
        unitLog.result_flag = BindTrafficInfo();
        searchPhone();
        unitLogBll.AddUnitLog(unitLog);
        //以前没有查询过，并且此次查询成功，要求话务员输入电话号码保存。
        if (lblPhoneA.Text == "" && lblPhoneB.Text == "" && lblPhoneC.Text == "" && unitLog.result_flag)
        {
            Response.Write("<script>function OpenArea(url,name,iWidth,iHeight) { var url; var name; var iWidth; var iHeight; var iTop = (window.screen.availHeight - 30 - iHeight) / 2; var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; window.open(url,name,'height='+iHeight+',innerHeight='+iHeight+',width='+iWidth+'.innerWidth'+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');} OpenArea('unit.aspx?unitnum=" + unit_num + "&unitshelf=" + txtClsbdh.Text + "&action=add', 'newwindow', 400, 240);</script>");
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindTrafficInfo();
    }
    protected void txtHphm_TextChanged(object sender, EventArgs e)
    {
        //输入完号牌号码后
        if (txtHphm.Text.Length == 5)
        {
            unit_infoData unit = new unit_infoData();
            unit_infoBll unitbll = new unit_infoBll();
            string unit_num = ddlDy.SelectedValue + ddlXzqh.SelectedValue + txtHphm.Text;
            unit = unitbll.GetUnitDetail(unit_num);
            //以前查询过，有数据的显示出来
            if (unit.unit_shelf_number != null)
            {
                txtClsbdh.Text = unit.unit_shelf_number;
                lblPhoneA.Text = unit.phone1;
                lblPhoneB.Text = unit.phone2;
                lblPhoneC.Text = unit.phone3;
            }
            else
            {
                txtClsbdh.Text = "";
                lblPhoneA.Text = "";
                lblPhoneB.Text = "";
                lblPhoneC.Text = "";
            }
        }
    }

    
    #region Method

    /// <summary>
    /// 采集信息
    /// gw  2011/8/12
    /// </summary>
    /// <returns>查询是否成功：True or False</returns>
    public bool BindTrafficInfo()
    {
        #region 变量
        string retHphm = "";//号牌号码
        string retHpzl = "";//号牌种类
        string retWfsj = "";//违法时间
        string retWfje = "";//违法金额
        string retJf = "";//记分
        string retJkzt = "";//缴款状态
        string retClzt = "";//处理状态
        string retWfdd = "";//违法地点
        string retWfnr = "";//违法内容
        string retJdsbh = "";//决定书编号
        string retCjjg = "";//采集机关
        string cao = ""; // "veh"
        string ResultHTML = "";
        if (rbnCao.Checked == true)
        {
            cao = "veh";
        }
        else
        {
            cao = "drv";
        }
        string hpzl = ddlHpzl.SelectedValue; //"02";
        string dy = ddlDy.SelectedValue; //"黑";
        string xzqh = ddlXzqh.SelectedValue; //"A";
        string hphm = txtHphm.Text.Trim(); //"SD626";
        string clsbdh = txtClsbdh.Text.Trim(); //"LDC611P28A1329637";
        string jszh = ""; //txtJszh.Text.Trim();
        string dabh = ""; //txtDabh.Text.Trim();
        string jkbj = "";  //% 0
        if (rbnJkbj.Checked == true)
        {
            jkbj = "0";
        }
        else
        {
            jkbj = "%";
        }
        string ts = "1312601058813";
        List<Cls_Traffic> infoList = new List<Cls_Traffic>();
        #endregion

        #region 模拟请求
        try
        {
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.hljjj.gov.cn:8081/Home/QueryWf");2012-5-28因网站改版而修改
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.hljjj.gov.cn:8081/Home/getWfcx");
            request.Method = "post";
            //request.Referer = "http://www.hljjj.gov.cn:8081/";2012-5-28因网站改版而修改
            request.Referer = "http://www.hljjj.gov.cn:8081/Home/Wfcx";
            request.Accept = "application/json, text/javascript, */*";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Maxthon; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.Host = "www.hljjj.gov.cn:8081";

            //填充POST数据  
            System.IO.Stream RequsetStream = request.GetRequestStream();
            System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");//GB2312
            Byte[] PostData = encoding.GetBytes("cao=" + cao + "&hpzl=" + hpzl + "&dy=" + dy + "&xzqh=" + xzqh + "&hphm=" + hphm + "&clsbdh=" + clsbdh + "&jszh=" + jszh + "&dabh=" + dabh + "&jkbj=" + jkbj + "&ts=" + ts);

            RequsetStream.Write(PostData, 0, PostData.Length);
            RequsetStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            System.IO.StreamReader Reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
            ResultHTML = Reader.ReadToEnd();
        }
        catch
        {
            errinfo.InnerText = "网页连接失败！";
            dlInfo.DataSource = null;
            dlInfo.DataBind();
            AspNetPager1.RecordCount = infoList.Count;
            AspNetPager1.CustomInfoHTML = "共0条记录      1/1";
            return false;
        }
        #endregion

        #region 截取信息
        if (ResultHTML.IndexOf("[\"4\",\"") == 0)
        {
            errinfo.InnerText = "号牌号码或车辆识别代号填写错误!";
            dlInfo.DataSource = null;
            dlInfo.DataBind();
            AspNetPager1.RecordCount = infoList.Count;
            AspNetPager1.CustomInfoHTML = "共0条记录      1/1";
            return false;
        }
        else if (ResultHTML.IndexOf("数据库繁忙，请稍后再试") > 0)
        {
            errinfo.InnerText = "数据库繁忙，请稍后再试!";
            dlInfo.DataSource = null;
            dlInfo.DataBind();
            AspNetPager1.RecordCount = infoList.Count;
            AspNetPager1.CustomInfoHTML = "共0条记录      1/1";
            return false;
        }
        else
        {
            errinfo.InnerText = "";
            ResultHTML = ResultHTML.Replace("\\u0027", "'");
            ResultHTML = ResultHTML.Replace("\\u003c", "<");
            ResultHTML = ResultHTML.Replace("\\u003e", ">");
            //string sign = "showfxccl";2012-5-28因网站改版而修改
            string sign = "<table";
            if (ResultHTML.IndexOf(sign) > 0)
            {
                ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(sign));
                ResultHTML = ResultHTML.Substring(0, ResultHTML.IndexOf("\",null,null,null]"));
                //while (ResultHTML.IndexOf(sign) >= 0)2012-5-28因网站改版而修改
                while (ResultHTML.IndexOf("<tr") >= 0 && ResultHTML.IndexOf("<td>") >= 0)
                {
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf("<td>"));

                    retHphm = ResultHTML.Substring(ResultHTML.IndexOf("<td>") + "<td>".Length);
                    retHphm = retHphm.Substring(0, retHphm.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retHphm) + (retHphm + "</td><td>").Length);

                    retHpzl = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retHpzl) + (retHpzl + "</td><td>").Length);

                    retWfsj = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retWfsj) + (retWfsj + "</td><td>").Length);

                    retWfje = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retWfje) + (retWfje + "</td><td>").Length);

                    retJf = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retJf) + (retJf + "</td><td>").Length);

                    retJkzt = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retJkzt) + (retJkzt + "</td><td>").Length);

                    retClzt = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retClzt) + (retClzt + "</td><td>").Length);

                    retWfdd = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retWfdd) + (retWfdd + "</td><td>").Length);

                    retWfnr = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retWfnr) + (retWfnr + "</td><td>").Length);

                    retJdsbh = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retJdsbh) + (retJdsbh + "</td><td>").Length);

                    retCjjg = ResultHTML.Substring(0, ResultHTML.IndexOf("</td>"));
                    ResultHTML = ResultHTML.Substring(ResultHTML.IndexOf(retCjjg) + (retCjjg + "</td><td>").Length);

                    Cls_Traffic jtwz = new Cls_Traffic();
                    jtwz.hphm = retHphm;
                    jtwz.hpzl = retHpzl;
                    jtwz.wfsj = retWfsj;
                    jtwz.wfje = retWfje;
                    jtwz.jf = retJf;
                    jtwz.jkzt = retJkzt;
                    jtwz.clzt = retClzt;
                    jtwz.wfdd = retWfdd;
                    jtwz.wfnr = retWfnr;
                    jtwz.jdsbh = retJdsbh;
                    jtwz.cjjg = retCjjg;
                    infoList.Add(jtwz);
                }
            }
        }
        #endregion

        #region 绑定GridView
        AspNetPager1.RecordCount = infoList.Count;
        int subtract_number = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);
        List<Cls_Traffic> pageList = new List<Cls_Traffic>();
        int j = 0;
        int maxByPage = 0;
        if (AspNetPager1.RecordCount <= AspNetPager1.PageSize * AspNetPager1.CurrentPageIndex)
        {
            maxByPage = AspNetPager1.RecordCount;
        }
        else
        {
            maxByPage = subtract_number + AspNetPager1.PageSize;
        }
        if (AspNetPager1.CurrentPageIndex == 1)
        {
            j = subtract_number;
            maxByPage = maxByPage + 1;
        }
        else
        {
            j = subtract_number - 1;
        }
        for (int i = j; i < maxByPage - 1; i++)
        {
            pageList.Add(infoList[i]);
        }
        dlInfo.DataSource = pageList;
        dlInfo.DataBind();
        AspNetPager1.CustomInfoHTML = "共" + AspNetPager1.RecordCount + "条记录      " + AspNetPager1.CurrentPageIndex + "/" + AspNetPager1.PageCount;
        #endregion

        return true;
    }

    public void searchPhone()
    {
        unit_infoData unit = new unit_infoData();
        unit_infoBll unitbll = new unit_infoBll();
        string unit_num = ddlDy.SelectedValue + ddlXzqh.SelectedValue + txtHphm.Text;
        unit = unitbll.GetUnitDetail(unit_num);
        //以前查询过，有数据的显示出来
        if (unit.unit_shelf_number != null)
        {
            lblPhoneA.Text = unit.phone1;
            lblPhoneB.Text = unit.phone2;
            lblPhoneC.Text = unit.phone3;
        }
        else
        {
            lblPhoneA.Text = "";
            lblPhoneB.Text = "";
            lblPhoneC.Text = "";
        }
    }
    #endregion  
}
#region class
public class Cls_Traffic
{
    public string hphm { get; set; }
    public string hpzl { get; set; }
    public string wfsj { get; set; }
    public string wfje { get; set; }
    public string jf { get; set; }
    public string jkzt { get; set; }
    public string clzt { get; set; }
    public string wfdd { get; set; }
    public string wfnr { get; set; }
    public string jdsbh { get; set; }
    public string cjjg { get; set; }
}
#endregion